package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbcabDataOnDemand.
 */
@Configurable
@Component
public class TtlsbcabDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbcab> data;

	/**
	 * Gets the new transient ttlsbcab.
	 *
	 * @param index the index
	 * @return the new transient ttlsbcab
	 */
	public Ttlsbcab getNewTransientTtlsbcab(int index) {
        Ttlsbcab obj = new Ttlsbcab();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodCdnitr(obj, index);
        setCodDiisoalf(obj, index);
        setCodEstacash(obj, index);
        setCodSufprese(obj, index);
        setCodTipPagx(obj, index);
        setDesNomfich(obj, index);
        setDesReficher(obj, index);
        setFecBorrcash(obj, index);
        setFecEstacash(obj, index);
        setFecProcesca(obj, index);
        setQnuTotregis(obj, index);
        setQtyTotimp(obj, index);
        setXsnFormpago(obj, index);
        setXsnMedcreac(obj, index);
        setXsnModcreac(obj, index);
        setXsnMultirem(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbcab obj, int index) {
		TtlsbcabPK embeddedIdClass;
		if (String.valueOf(index).length() > 7){
			embeddedIdClass = new TtlsbcabPK("c" + index, "c" + String.valueOf(index).substring(0, 2), "c" + String.valueOf(index).substring(0, 6));
		}else{
			embeddedIdClass = new TtlsbcabPK("c" + index, "c" + index, "c" + index);
		}
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbcab obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbcab obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod cdnitr.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodCdnitr(Ttlsbcab obj, int index) {
        String codCdnitr = "codCdnit_" + index;
        if (codCdnitr.length() > 10) {
            codCdnitr = codCdnitr.substring(0, 10);
        }
        obj.setCodCdnitr(codCdnitr);
    }

	/**
	 * Sets the cod diisoalf.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodDiisoalf(Ttlsbcab obj, int index) {
        String codDiisoalf = "c_" + index;
        if (codDiisoalf.length() > 3) {
            codDiisoalf = codDiisoalf.substring(0, 3);
        }
        obj.setCodDiisoalf(codDiisoalf);
    }

	/**
	 * Sets the cod estacash.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodEstacash(Ttlsbcab obj, int index) {
        String codEstacash = "c_" + index;
        if (codEstacash.length() > 3) {
            codEstacash = codEstacash.substring(0, 3);
        }
        obj.setCodEstacash(codEstacash);
    }

	/**
	 * Sets the cod sufprese.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSufprese(Ttlsbcab obj, int index) {
        String codSufprese = "c_" + index;
        if (codSufprese.length() > 3) {
            codSufprese = codSufprese.substring(0, 3);
        }
        obj.setCodSufprese(codSufprese);
    }

	/**
	 * Sets the cod tip pagx.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodTipPagx(Ttlsbcab obj, int index) {
        String codTipPagx = "c" + index;
        if (codTipPagx.length() > 2) {
            codTipPagx = codTipPagx.substring(0, 2);
        }
        obj.setCodTipPagx(codTipPagx);
    }

	/**
	 * Sets the des nomfich.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesNomfich(Ttlsbcab obj, int index) {
        String desNomfich = "desNomfich_" + index;
        if (desNomfich.length() > 20) {
            desNomfich = desNomfich.substring(0, 20);
        }
        obj.setDesNomfich(desNomfich);
    }

	/**
	 * Sets the des reficher.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesReficher(Ttlsbcab obj, int index) {
        String desReficher = "desReficher_" + index;
        if (desReficher.length() > 30) {
            desReficher = desReficher.substring(0, 30);
        }
        obj.setDesReficher(desReficher);
    }

	/**
	 * Sets the fec borrcash.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecBorrcash(Ttlsbcab obj, int index) {
        String fecBorrcash = "fecBor_" + index;
        if (fecBorrcash.length() > 8) {
            fecBorrcash = fecBorrcash.substring(0, 8);
        }
        obj.setFecBorrcash(fecBorrcash);
    }

	/**
	 * Sets the fec estacash.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecEstacash(Ttlsbcab obj, int index) {
        String fecEstacash = "fecEst_" + index;
        if (fecEstacash.length() > 8) {
            fecEstacash = fecEstacash.substring(0, 8);
        }
        obj.setFecEstacash(fecEstacash);
    }

	/**
	 * Sets the fec procesca.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecProcesca(Ttlsbcab obj, int index) {
        String fecProcesca = "fecPro_" + index;
        if (fecProcesca.length() > 8) {
            fecProcesca = fecProcesca.substring(0, 8);
        }
        obj.setFecProcesca(fecProcesca);
    }

	/**
	 * Sets the qnu totregis.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setQnuTotregis(Ttlsbcab obj, int index) {
        Integer qnuTotregis = new Integer(index);
        obj.setQnuTotregis(qnuTotregis);
    }

	/**
	 * Sets the qty totimp.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setQtyTotimp(Ttlsbcab obj, int index) {
        BigDecimal qtyTotimp = BigDecimal.valueOf(index);
        if (qtyTotimp.compareTo(new BigDecimal("999999999999999.99")) == 1) {
            qtyTotimp = new BigDecimal("999999999999999.99");
        }
        obj.setQtyTotimp(qtyTotimp);
    }

	/**
	 * Sets the xsn formpago.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnFormpago(Ttlsbcab obj, int index) {
        Character xsnFormpago = new Character('N');
        obj.setXsnFormpago(xsnFormpago);
    }

	/**
	 * Sets the xsn medcreac.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnMedcreac(Ttlsbcab obj, int index) {
        Character xsnMedcreac = new Character('N');
        obj.setXsnMedcreac(xsnMedcreac);
    }

	/**
	 * Sets the xsn modcreac.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnModcreac(Ttlsbcab obj, int index) {
        Character xsnModcreac = new Character('N');
        obj.setXsnModcreac(xsnModcreac);
    }

	/**
	 * Sets the xsn multirem.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnMultirem(Ttlsbcab obj, int index) {
        Character xsnMultirem = new Character('N');
        obj.setXsnMultirem(xsnMultirem);
    }

	/**
	 * Gets the specific ttlsbcab.
	 *
	 * @param index the index
	 * @return the specific ttlsbcab
	 */
	public Ttlsbcab getSpecificTtlsbcab(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbcab obj = data.get(index);
        return Ttlsbcab.findTtlsbcab(obj.getId());
    }

	/**
	 * Gets the random ttlsbcab.
	 *
	 * @return the random ttlsbcab
	 */
	public Ttlsbcab getRandomTtlsbcab() {
        init();
        Ttlsbcab obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbcab.findTtlsbcab(obj.getId());
    }

	/**
	 * Modify ttlsbcab.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbcab(Ttlsbcab obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbcab.findTtlsbcabEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbcab' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbcab>();
        for (int i = 0; i < 10; i++) {
            Ttlsbcab obj = getNewTransientTtlsbcab(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
